#!/usr/bin/perl
#
#

use Data::Dumper;

sub expand {
	my $s = shift;
	$s =~ s/S/SlSlS/g;
	$s =~ s/L/SLSLSLS/g;
	$s =~ s/l/L/g;
	$s;
}

my $times = int shift @ARGV;
my $sqrt2 = sqrt 2;
my $scale = 3 + 2 * sqrt 2;

my $o = 'LS';
my %k = (
	L => $sqrt2,
	S => 1,
);
my @p = ();
for (0 .. $times) {
	my @i = split '', $o;
	my $n = 0;
	my @o;
	push @o, $n;
	for (@i) {
		$n += $k{$_};
		push @o, $n;
	}
	#$n /= 2;
	#@o = map { $_ - $n } @o;
	push @p, [map {int ($_ * 1024) } @o];
	#print join ' ', @o;
	#print "\n";
	last if $_ == $times;
	$o = expand $o;
	$k{$_} /= $scale for keys %k;
}

my @b = ();
my $c = 255;
for (@p) {
	#print join ' ', @$_;
	#print "\n";
	for (@$_) {
		next unless $_ >= 0;
		next if $b[$_];
		$b[$_] = $c;
	}
	$c = int $c / 2;#$delta_s;
}

@b = map {$_ ? $_ : 0} @b;
my $w = scalar @b;
print qq{P2
$w 1
255
@b
};
